import 'package:flutter/material.dart';
import 'package:get/get.dart';
import 'package:sainbainu/common/utils/loading.dart';
import 'package:sainbainu/common/values/colors.dart';
import 'package:sainbainu/common/widgets/cancel_confirm_wrapper.dart';

class IntroDialog extends StatefulWidget {
  final String? value;
  final VoidCallback? onCancelTap;
  final void Function(String value)? onConfirmTap;

  const IntroDialog({
    super.key,
    this.value,
    this.onCancelTap,
    this.onConfirmTap,
  });

  @override
  State<IntroDialog> createState() => _IntroDialogState();
}

class _IntroDialogState extends State<IntroDialog> {
  String? _value;

  @override
  void initState() {
    super.initState();
    _value = widget.value;
  }

  /// 点击确认
  void handleConfirm() {
    if (_value == null || _value!.isEmpty) {
      return LoadingUtil.showInfo('${'简介'.tr}不能为空');
    }
    if (widget.onConfirmTap != null) {
      widget.onConfirmTap!(_value!);
    }
  }

  /// 收集输入内容
  void onChanged(String value) {
    _value = value;
    setState(() {});
  }

  @override
  Widget build(BuildContext context) {
    return CancelConfirmWrapper(
      titleText: '${'设置'.tr}${'简介'.tr}',
      onCancelTap: widget.onCancelTap,
      onConfirmTap: _value == widget.value ? null : handleConfirm,
      child: Padding(
        padding: const EdgeInsets.all(15),
        child: Column(
          children: [
            customTextField(),
          ],
        ),
      ),
    );
  }

  /// 自定义输入框
  Widget customTextField() {
    return TextFormField(
      initialValue: _value,
      keyboardType: TextInputType.text,
      maxLength: 120,
      maxLines: 3,
      cursorColor: AppColors.primary,
      decoration: InputDecoration(
        hintText: '介绍喜好或个性，让更多人了解你'.tr,
        helperStyle: const TextStyle(
          color: AppColors.unactive,
        ),
        focusedBorder: const UnderlineInputBorder(
          borderSide: BorderSide(color: AppColors.primary),
        ),
        enabledBorder: const UnderlineInputBorder(
          borderSide: BorderSide(color: AppColors.border),
        ),
        errorText: null,
      ),
      onChanged: onChanged,
    );
  }
}
